﻿using DtuService;
using System.Text;
using NetCommand.Web;
using Busness;

class login : HtmPage
{
    private LoginResult loginResult = new LoginResult();
    private RegistUserResult registerResult;

    public override void OnPageProcess(WebMessage msg)
    {
        switch(msg.CLSID)
        {
            case WebMessageDefine.id_GetIdentifyCode:
                {
                    handleGetRegistCode(msg);
                }
                break;
            case WebMessageDefine.id_RegisterUser:
                {
                    handleRegisterUser(msg);
                }
                break;
            case WebMessageDefine.id_ModifyPwd:
                {
                    handleChangePwd(msg);
                }
                break;
            case WebMessageDefine.id_LoginMessage:
                {
                    doLogin(msg);
                }
                break;
            default:
                break;
        }
        
    }

    // 注册
    private void handleRegisterUser(WebMessage msg)
    {
        RegisterUser regUser = msg as RegisterUser;
        if (regUser == null)
        {
            httpClient.NotImplemented();
            return;
        }

        registerResult = new RegistUserResult();
        if (!ServerMain.GetServer<CacheServer>().CheckIdentityCode(regUser.UserName, regUser.IdentifyCode))
        {
            // 验证码错误
            registerResult.ErrorCode = WebCodeDefine.REGIST_IDENTIFY_CODE_WRONG;
            sendOkResponse(Encoding.UTF8.GetBytes(serializeMessage(registerResult)));
            return;
        }

        Busness.Business.getInstance().registUser(regUser.UserName, regUser.Password, regUser.NickName, new DataAccess.ExcuteSqlEventHander((object sender, DataAccess.ExcuteSqlEventArgs e) =>
        {
            int result = (int)e._returnValue;
            if (result == SqlErrorCode.REGIST_USER_HAS_USER)
            {
                registerResult.ErrorCode = WebCodeDefine.REGIST_USER_HAS_USER;
            }
            else
            {
                registerResult.ErrorCode = WebCodeDefine.OK;
            }
            sendOkResponse(Encoding.UTF8.GetBytes(serializeMessage(registerResult)));
        }));
        // TODO 把单位也存库
    }

    private void handleGetRegistCode(WebMessage msg)
    {
        GetIdentifyCode getCode = msg as GetIdentifyCode;
        if (getCode == null)
        {
            httpClient.NotImplemented();
            return;
        }
        string registCode = ServerMain.Instance.CommonTools.Get6RandomChar();
        ServerMain.GetServer<CacheServer>().AddPhoneRegCode(getCode.PhoneNumber, registCode);
        // TODO 通过短信商发到用户手机
    }

    // 修改密码
    private void handleChangePwd(WebMessage msg)
    {
        ModifyPassword mdPwd = msg as ModifyPassword;
        if(mdPwd == null)
        {
            httpClient.Shutdown();
            return;
        }
        
        ModifyPasswordResult mdRes = new ModifyPasswordResult();
        if (mdPwd.RepPwd != mdPwd.NewPwd)
        {
            mdRes.ErrorCode = WebCodeDefine.REP_PWD_NOT_EQU_NEW_PWD;
            sendOkResponse(Encoding.UTF8.GetBytes(serializeMessage(mdRes)));
        }
        else
        {
            Business.getInstance().changePwd(mdPwd.NewPwd, mdPwd.OldPwd, userID, new DataAccess.ExcuteSqlEventHander((object sender, DataAccess.ExcuteSqlEventArgs e) =>
            {
                int result = (int)e._returnValue;
                if (result == SqlErrorCode.EXECUTE_OK)
                {
                    mdRes.ErrorCode = WebCodeDefine.OK;
                }
                else if (result == SqlErrorCode.CHANGE_PWD_NO_USER)
                {
                    mdRes.ErrorCode = WebCodeDefine.CHANGE_PWD_NO_USER;
                }
                else if (result == SqlErrorCode.CHANGE_PWD_OLDPWD_WRONG)
                {
                    mdRes.ErrorCode = WebCodeDefine.CHANGE_PWD_OLD_WRONG;
                }
                sendOkResponse(Encoding.UTF8.GetBytes(serializeMessage(mdRes)));
            }));
        }
    }

    // 登录
    private void doLogin(WebMessage msg)
    {
        LoginMessage loginMsg = (LoginMessage)msg;
        Busness.Business.getInstance().checkPwd(loginMsg.userName, loginMsg.passWord,new DataAccess.ExcuteSqlEventHander(
        (object sender, DataAccess.ExcuteSqlEventArgs e) =>
        {
            int result = (int)e._returnValue;

            if (result == Busness.SqlErrorCode.NO_USER)
            {
                loginResult.ErrorCode = WebCodeDefine.NO_USER;
                loginResult.Token = "";
            }
            else if (result == Busness.SqlErrorCode.PASSWORD_WRONG)
            {
                loginResult.ErrorCode = WebCodeDefine.PASSWORD_WRONG;
                loginResult.Token = "";
            }
            else
            {
                Token = ServerMain.GetServer<CacheServer>().CreateTokenWithUserID((uint)result, httpClient.bSession.IPAddress);
                loginResult.Token = Token;
                loginResult.ErrorCode = WebCodeDefine.OK;
            }
            sendOkResponse(Encoding.UTF8.GetBytes(serializeMessage(loginResult)));

        }));
    }
}

